package com.woniu.controller.mallSystem;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.woniu.model.gh.common.Result;
import com.woniu.model.mallSystem.dto.CategoryTypeDto;
import com.woniu.model.mallSystem.entity.Category;
import com.woniu.model.mallSystem.gh.annotation.UpdateCommodityTypeCacheAnnotation;
import com.woniu.model.mallSystem.gh.common.RedisConstant;
import com.woniu.model.mallSystem.vo.CategoryVo;
import com.woniu.service.mallSystem.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 品类表 前端控制器
 * </p>
 */
@Api(tags = "品类管理")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private ICategoryService categoryService;
    /**
     * 商品分类
     */
    @PostMapping("/findAllCategory")
    @ApiOperation("多条件搜索品类")
    public Result findAll(@RequestBody Map<String,Object> condition){
        int pageNum = (int) condition.get("pageNum");
        int pageSize = (int) condition.get("pageSize");
        PageInfo<Category> pageInfo = categoryService.findCategories(pageNum, pageSize,condition);
        return Result.ok(pageInfo);
    }

    /**
     *封装条件查询
     */
    @PostMapping("/findAllCategory2")
    @ApiOperation("封装动态查询")
    public Result findAllType(@RequestBody Map<String,Object> condition){
        int pageNum = (int) condition.get("pageNum");
        int pageSize = (int) condition.get("pageSize");
        PageInfo<CategoryVo> pageInfo = categoryService.findAllType(pageNum, pageSize,condition);
        return Result.ok(pageInfo);
    }
    /**
     * 查询所有一级品类
     */
    @GetMapping("/findAllCategoryName")
    @ApiOperation("查询一级品类")
    public Result findAllCategoryName(){
        if(!redisTemplate.hasKey(RedisConstant.CATEGORY_ALL_PLIST)){
            LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery(Category.class).eq(Category::getParentCategoryId, -1).select(Category::getId, Category::getCategoryName);
            List<Category> list = categoryService.list(wrapper);
            List<String> result = list.stream().map(Category::getCategoryName).collect(Collectors.toList());
            //存入redis
            redisTemplate.opsForList().rightPushAll(RedisConstant.CATEGORY_ALL_PLIST,result);
            return Result.ok(result);
        }
        //redis获取数据
        List list = redisTemplate.opsForList().range(RedisConstant.CATEGORY_ALL_PLIST, 0, -1);
        return Result.ok(list);
    }
    /**
     * 根据品类id查询所有子品类
     */
    @GetMapping("/getAllChildrenCategory")
    @ApiOperation("根据id查询子品类")
    public Result getAllChildrenCategory(Integer id){
        List<Category> childrenCategory = categoryService.getAllChildrenCategory(id);
        return Result.ok(childrenCategory);
    }
    /**
     * 查看品类详情
     */
    @GetMapping("/viewCategoryDetails")
    @ApiOperation("查看详情")
    public Result getCategoryDetails(Integer id){
        if (!redisTemplate.hasKey(RedisConstant.CATEGORY_DETAIL)){
            Category category = categoryService.getById(id);
            //存入redis
            redisTemplate.opsForHash().put(RedisConstant.CATEGORY_DETAIL+id,id.toString(),JSONUtil.toJsonStr(category));
            return Result.ok(category);
        }
        //获取数据
        String json = (String) redisTemplate.opsForHash().get(RedisConstant.CATEGORY_DETAIL+id, id.toString());
        Category category = JSONUtil.toBean(json, Category.class);
        return Result.ok(category);
    }
    /**
     * 添加或修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation("添加/修改品类")
    public Result saveOrUpdate(@RequestBody Category category){
        categoryService.saveOrUpdate(category);
        return Result.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/deleteById")
    @ApiOperation("根据id删除品类")
    public Result deleteById(Integer id){
        categoryService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    @ApiOperation("批量删除品类")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        categoryService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 批量下架
     */
    @PostMapping("/updateStatus")
    @ApiOperation("批量下架")
    public Result<Integer> updateProduct(@RequestBody List<Long> ids) {
        List<Category> list = categoryService.listByIds(ids);
        for (Category category : list) {
            category.setStatus(0);
        }
        categoryService.saveOrUpdateBatch(list);
        return Result.ok();
    }

    @PostMapping("/add")
    @ApiOperation("添加品类")
    @UpdateCommodityTypeCacheAnnotation
    public Result add(@RequestBody CategoryTypeDto categoryTypeDto){
        categoryService.addEntity(categoryTypeDto);
        return Result.ok();
    }
    @PutMapping("/update")
    @ApiOperation("更新品类")
    public Result update(@RequestBody CategoryTypeDto categoryTypeDto){
        categoryService.updateCategory(categoryTypeDto);
        return Result.ok();
    }


}
